#define _CRT_SECURE_NO_WARNINGS  1


class Solution
{
public:
    int triangleNumber(vector<int>& nums)
    {
        sort(nums.begin(), nums.end());

        int count = 0;
        int max = nums.size() - 1;
        while (max)
        {
            int left = 0;
            int right = max - 1;
            while (left < right)
            {
                if (nums[left] + nums[right] > nums[max])
                {
                    count += (right - left);
                    right--;
                }
                else
                {
                    left++;
                }
            }
            max--;
        }
        return count;
    }
};